<template>
  <PanelHead :route="route" />
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id" />
    <el-table-column prop="name" label="昵称" />
    <el-table-column prop="permissions_id" label="所属组别">
      <template #default="scope">
        {{ permissiosName(scope.row.permissions_id) }}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号" />
    <el-table-column prop="active" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.active == 1 ? 'success' : 'danger'">
          {{ scope.row.active == 1 ? '正常' : '失效' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column label="创建时间">
      <template #default="scope">
        <div class="create-time">
          <el-icon>
            <Clock />
          </el-icon>
          <span style="margin-left: 10px;">{{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="pagination-info">
    <el-pagination v-model:current-page="paginationData.pageNum" :page-size="paginationData.pageSize"
      :background="false" layout="total, prev, pager, next" :total="tableData.total" size="small"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <!-- 弹窗 -->
  <el-dialog v-model="dialogFormVisible" :before-close="beforeClose" title="添加权限" width="500px">
    <el-form ref="formRef" label-width="100px" label-position="left" :model="form" :rules="rules">
      <el-form-item label="手机号" prop="mobile">
        <!-- disabled 禁用 -->
        <el-input v-model="form.mobile" disabled />
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="菜单权限" prop="permissions_id">
        <el-select v-model="form.permissions_id" placeholder="请选择菜单权限" style="width: 240px">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { getAuthAdmin, menuSelectlist, userUpdate } from '../../../api/index'
import { reactive, onMounted, ref, nextTick } from 'vue'
import dayjs from 'dayjs'
import type { FormInstance } from 'element-plus'
import { useRoute } from 'vue-router'

// 获取pinia数据
const route = useRoute();

const paginationData = reactive({
  pageNum: 1,
  pageSize: 10
});
// 列表数据
const tableData = ref({
  list: [],
  total: 0
});
interface tableDatalistType {
  active: number,
  create_time: number | string,
  id: number,
  mobile: string,
  name: string,
  permissions_id: number
}

onMounted(() => {
  getListData()
});

// 请求列表
const getListData = () => {
  getAuthAdmin(paginationData).then(({ data }) => {
    // 处理响应数据
    const { list, total } = data.data
    list.forEach((item: tableDatalistType) => {
      item.create_time = dayjs(item.create_time).format('YYYY-MM-DD')
    })
    tableData.value.list = list
    tableData.value.total = total
  });
  menuSelectlist().then(({ data }) => {
    // 处理响应数据
    options.value = data.data
  });
}

interface optionsType {
  id: number,
  name: string
}
// 菜单权限下拉
const options = ref<optionsType[]>([])

// 根据权限id匹配权限名称
const permissiosName = (id: number) => {
  const data = options.value.find(el => el.id === id)
  return data ? data.name : '超级管理员'
}

// 分页控制
const handleSizeChange = (val: number) => {
  paginationData.pageSize = val;
}
const handleCurrentChange = (val: number) => {
  paginationData.pageNum = val;
}

// 弹窗
interface formType {
  mobile: string,
  name: string,
  permissions_id: string
}
// 表单校验数据
const formRef = ref<FormInstance>()
// 表单数据
const form: formType = reactive({
  mobile: '',
  name: '',
  permissions_id: ''
});
// 表单校验 trigger:失去焦点的时候 
const rules = reactive({
  name: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
  permissions_id: [{ required: true, message: '请选择菜单权限', trigger: 'change' }]
})
// 弹窗的显示隐藏
const dialogFormVisible = ref(false);
// 打开弹窗
const open = (rowData: formType) => {
  dialogFormVisible.value = true;
  Object.assign(form, { mobile: rowData.mobile, name: rowData.name, permissions_id: rowData.permissions_id });
}
// 关闭弹窗的回调
const beforeClose = () => {
  dialogFormVisible.value = false;
}
// 表单提交
const confirm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  // 手动触发表单校验
  await formEl.validate((valid, fields) => {
    if (valid) {
      const { name, permissions_id } = form
      // 提交表单
      userUpdate({ name, permissions_id }).then(({ data }) => {
        if (data.code === 10000) {
          dialogFormVisible.value = false;
          getListData()
        }
      })
    } else {
      // 处理表单校验失败
      console.log('error submit!', fields)
    }
  })
}
</script>

<style scoped lang="less">
.create-time {
  display: flex;
  align-items: center;
}
</style>